package project1.controller;

import com.google.gson.Gson;
import project1.model.Orders;
import project1.model.Result;
import project1.model.bo.PageOrderBO;
import project1.service.OrderService;
import project1.service.OrderServiceImpl;
import project1.utils.HtttpUtils;
import project1.view.PageOrdersVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/admin/order/*")
public class OrdersServlet extends HttpServlet {
    private Gson gson = new Gson();
    private OrderService orderService = new OrderServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
        if ("ordersByPage".equals(action)){
         ordersBypage(request,response);
        }
    }

    /**
     * 根据条件分页查询显示订单
     * 1.获取请求参数
     * 2.业务处理
     * 3.响应
     * @param request
     * @param response
     */
    private void ordersBypage(HttpServletRequest request, HttpServletResponse response) throws IOException {
       //校验
        String requestBody = HtttpUtils.getRequestBody(request);
        PageOrderBO orderBO = gson.fromJson(requestBody, PageOrderBO.class);
        List<PageOrdersVO> orders = orderService.ordersByPage(orderBO);
        response.getWriter().println(gson.toJson(Result.ok(orders)));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
    }
}
